package com.nowcoder.community.service;

import com.nowcoder.community.mapper.DiscussPostMapper;
import com.nowcoder.community.mapper.UserMapper;
import com.nowcoder.community.pojo.DiscussPost;
import com.nowcoder.community.pojo.User;
import com.nowcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Date;

@Service
public class TestService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    DiscussPostMapper discussPostMapper;

    @Autowired
    TransactionTemplate transactionTemplate;

    /*
    REQUIRED：支持当前事务（外部事务），如果不存在就创建新的事务
             （例如：事务A调用事务B，则当前事务就是事务A，也就是事务B的外部事务）

    REQUIRES_NEW：事务B被外部事务调用，无论这个外部事务是否存在，都会创建一个新事务。
                 （当前事务存在时会暂停这个事务）

    NESTED：外部事务调用事务B。如果当前事务存在，事务B就会嵌套在外部事务中；
            如果外部事务不存在，就会创建新的事务并支持新事务
    */

    //声明式事务
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void save(){
        //添加用户
        User user = new User();
        user.setUsername("AA");
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setPassword(CommunityUtil.md5("12345" + user.getSalt()));
        user.setEmail("abc@163.com");
        user.setHeaderUrl("http://images.nowcoder.com/head/1t.png");
        user.setCreateTime(new Date());
        userMapper.addUser(user);

        //添加帖子
        DiscussPost discussPost = new DiscussPost();
        discussPost.setUserId(user.getId());
        discussPost.setTitle("A");
        discussPost.setContent("我是新用户。");
        discussPost.setCreateTime(new Date());
        discussPostMapper.addDiscussPost(discussPost);

        int i = 1/0;
    }

    //编程式事务
    public Object save1(){
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        return transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                //添加用户
                User user = new User();
                user.setUsername("BB");
                user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
                user.setPassword(CommunityUtil.md5("12345" + user.getSalt()));
                user.setEmail("cba@163.com");
                user.setHeaderUrl("http://images.nowcoder.com/head/1t.png");
                user.setCreateTime(new Date());
                userMapper.addUser(user);

                //添加帖子
                DiscussPost discussPost = new DiscussPost();
                discussPost.setUserId(user.getId());
                discussPost.setTitle("A");
                discussPost.setContent("我是新用户。");
                discussPost.setCreateTime(new Date());
                discussPostMapper.addDiscussPost(discussPost);

                int i = 1/0;

                return "ok";
            }
        });
    }


}
